package com.zzmx.springboot.atomikos.web.controller;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@EnableTransactionManagement
public class TestController {

	@Autowired
	private JdbcTemplate fiveJdbcTemplate;

	@Autowired
	private JdbcTemplate sixJdbcTemplate;


	@Autowired
	private JdbcTemplate threeJdbcTemplate;


	//@Autowired
	//TransactionInterceptor transactionInterceptor; 设置事物管理器
	@RequestMapping("/test")
	@Transactional(isolation = Isolation.DEFAULT ,propagation = Propagation.REQUIRED)
	public void test() {
		System.out.println("begin.....");
		//Assert.assertEquals("1", busJdbcTemplate.queryForObject("select count(1) from bus_b", String.class));
		fiveJdbcTemplate.execute("insert into five(id) values(2)");
		sixJdbcTemplate.execute("insert into six(id) values(3)");
		sixJdbcTemplate.execute("insert into six(id) values(3)");
		System.out.println("end.....");
	}
	@RequestMapping("/test1")
	//@Transactional(isolation = Isolation.DEFAULT)
	public void test1() {
	//	busJdbcTemplate.execute("insert into bus_b(id) values(1)");
		test();
	}
	@RequestMapping("/order")
	//@Transactional(rollbackFor=Exception.class,isolation = Isolation.DEFAULT ,propagation = Propagation.REQUIRED)
	//(isolation = Isolation.DEFAULT ,propagation = Propagation.REQUIRED)
	@Transactional(value = "atomikosTransactionManager")
	public void test2() {
		System.out.println("begin.....");
		threeJdbcTemplate.execute("insert into three(id) values(1)");
        threeJdbcTemplate.execute("insert into three(id) values(2)");

		fiveJdbcTemplate.execute("insert into five(id) values(3)");
		fiveJdbcTemplate.execute("insert into five(id) values(4)");
		sixJdbcTemplate.execute("insert into six(id) values(5)");
		sixJdbcTemplate.execute("insert into six(id) values(6)");


		System.out.println("end.....");
	}

}
